package b4.view
{
	import b4.ApplicationFacade;
	import b4.model.SmartBirdExtension;
	import b4.view.component.admin.Painter;
	import b4.view.component.events.ItemClickEvent;
	import b4.view.component.pack.PackageDialog;
	
	import flash.events.Event;
	
	import org.aswing.event.PopupEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class PackageDialogMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'PackageDialogMediator';
		public static const ITEM_TYPE_CLICKED:String = "itemTypeClicked";
		public static const REFRESH_PACKAGE:String = "refreshPackage";
		
		public function PackageDialogMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				ApplicationFacade.PACKAGE_LOADED,
				PackageDialogMediator.ITEM_TYPE_CLICKED,
				PackageDialogMediator.REFRESH_PACKAGE
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch ( name )
			{
				case ApplicationFacade.PACKAGE_LOADED:
					packageDialog.refreshPackage(simpleClient.vo.me.pack);
					break;
				case PackageDialogMediator.ITEM_TYPE_CLICKED:
					var typeIdx:int = body as int;
					packageDialog.refreshPackage(simpleClient.vo.me.pack, typeIdx);
					break;
				case PackageDialogMediator.REFRESH_PACKAGE:
					packageDialog.refreshPackage(simpleClient.vo.me.pack);
					break;
			}
		}
		
		override public function onRegister():void
		{
			packageDialog.show();
			packageDialog.addEventListener(PopupEvent.POPUP_CLOSED, onPopUpClosed);
			packageDialog.addEventListener(ItemClickEvent.ITEM_CLICK, onItemClicked);
			packageDialog.addEventListener(ItemClickEvent.ITEM_MOUSE_OVER, onItemMouseOver);
			packageDialog.addEventListener(ItemClickEvent.ITEM_MOUSE_OUT, onItemMouseOut);
			packageDialog.addEventListener(ItemClickEvent.MENU_ITEM_CLICK, onMenuItemTypeClicked);
			simpleClient.getPackage();
		}
		
		private function onMenuItemTypeClicked(e: ItemClickEvent):void
		{
			facade.sendNotification(PackageDialogMediator.ITEM_TYPE_CLICKED, e.data);
		}

		private function onItemMouseOver(e:ItemClickEvent):void
		{
			facade.sendNotification(ApplicationMediator.SHOW_MOUSE_OVER_TIP, e.data);
		}
		
		private function onItemMouseOut(e:ItemClickEvent):void
		{
			facade.sendNotification(ApplicationMediator.HIDE_MOUSE_OVER_TIP);
		}
		
		private function onItemClicked(e:ItemClickEvent):void
		{
			facade.sendNotification(SmithyMediator.LOAD_ITEM_IMG, e.data);
			facade.sendNotification(HumanBeingMediator.LOAD_EQUIPMENT, e.data);
		}
		
		private function onPopUpClosed(event:Event):void
		{
			facade.removeMediator(NAME);
		}
		
		override public function onRemove():void
		{
			packageDialog.dispose();
			Painter.penMode = null;
		}
		
		private function get packageDialog():PackageDialog
		{
			return viewComponent as PackageDialog;
		}
		
		private function get simpleClient():SmartBirdExtension
		{
			return facade.retrieveProxy( SmartBirdExtension.NAME ) as SmartBirdExtension;
		}
	}
}